package com.ysd.lis.controller.sys;


import cn.hutool.core.bean.BeanUtil;
import com.ysd.common.Result;
import com.ysd.common.dto.OrganizationDTO;
import com.ysd.lis.annotation.LogDetail;
import com.ysd.lis.aspect.OperationType;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.SysModule;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.request.ModuleAddParam;
import com.ysd.lis.request.ModulePageParam;
import com.ysd.lis.service.SysModuleService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * cold_chain.sys_module 前端控制器
 * </p>
 *
 * @author Bai
 * @since 2023-10-18
 */
@Api(tags = "模块管理")
@RestController
public class SysModuleController extends BaseController {

    @Autowired
    SysModuleService sysModuleService;

    @ApiOperation("分页获取模块信息")
    @GetMapping("/api/sys/getPageModules")
    public Result getModules( ModulePageParam modulePageParam) {
        return sysModuleService.getPageModules(modulePageParam);
    }
//根据名称与code模糊查询
    @ApiOperation("获取模块信息（不分页)")
    @GetMapping("/api/sys/getModuleList")
    public Result getModuleList(@RequestParam(required = false) String keywords) {
        return sysModuleService.getModuleList(keywords);
    }

    @ApiOperation("添加模块")
    @PostMapping("/api/sys/addModule")
    @LogDetail(requestParam ="addModule",operationType = OperationType.INSERT, module = "模块管理",content = "新增模块操作")
    public Result addModule(@RequestBody ModuleAddParam moduleAddParam) {
        Result result = new Result();
        try {
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isEmpty(sysUser)) {
                return result.fail("从Redis获取用户数据为NULL: 请检查redis相关配置");
            }
            if (ToolsUtils.isEmpty(sysUser.getOrgId())){
                return result.fail("获取机构ID异常 ： 请给登录用户设置所属机构");
            }
            moduleAddParam.setOrgId(sysUser.getOrgId());
        } catch (Exception e) {
            // 处理异常情况
            result.fail("从Redis获取用户数据出现异常: 请检查redis相关配置");
        }
        Map returnMap = new HashMap<>();
        SysModule sysModule = new SysModule();
        BeanUtil.copyProperties(moduleAddParam, sysModule);
        String id  = sysModuleService.addModule(sysModule);
        returnMap.put("healthCheckId",id);
        return result.succ(1,"添加模块成功",returnMap);
    }

    @ApiOperation("修改模块")
    @PostMapping("/api/sys/editModule")
    @LogDetail(requestParam ="editModule",operationType = OperationType.UPDATE, module = "模块管理",content = "修改模块操作")
    public Result editModule(@RequestBody ModuleAddParam moduleAddParam) {
        SysModule sysModule = new SysModule();
        BeanUtil.copyProperties(moduleAddParam, sysModule);
        return sysModuleService.editModule(sysModule);
    }

    @ApiOperation("删除模块")
    @DeleteMapping("/api/sys/deleteModule/{id}")
    @LogDetail(requestParam ="deleteModule",operationType = OperationType.DELETE, module = "模块管理",content = "删除模块操作")
    public Result deleteModule(@PathVariable("id") String id) {
        return sysModuleService.deleteModle(id);
    }

    @ApiOperation("添加模块")
    @PostMapping("/api/sys/copyModuleAndMenuByOrgId")
    public Result addModule(@RequestBody OrganizationDTO moduleAddParam) {
        return sysModuleService.copyModuleAndMenuByOrgId(moduleAddParam);
    }

}
